/*************************************************
 *FileName:      redemption.js
 *Description:   兑换商品.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: true           //是否启用回弹 默认true
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    /*
     * 主程序
     */ 
    window.addEventListener('refreshmyprop', function(e){//执行刷新
        location.reload(); 
    });
    
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        mui.plusReady(getlist)
        $('.redemption').on('tap','.delete',function(){
            var othis =$(this).parent();
            mui.confirm('确定删除该兑换记录？','提示',['取消','确定'],function(e){ 
                e.index==1? deletlist(othis) : null;
            },'div');
        })
        
        $('.redemption').on('tap','.ads',function(){
            var orderid=$(this).parents('.mf-table-view-cell').attr('data-order');
            common.pageTransition('site.html?orderid='+orderid)
        })
    }


//获取信息
function getlist(){
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/order/usersOrders",
        dataType:'jsonp',
        data: { uid: uid, token: token},
        success: function(data) {
            if(data.error_code == 0){
                console.log(data)
                var ohtml ='';
                for(var i=0 ; i<data.data.length ;i++){
                    var otime =timemsg(data.data[i].time);
                    if(data.data[i].address_id=='0'){
                        ohtml+='<a class="mf-table-view-cell add" data-order="'+data.data[i].order_id+'" ><h3 class="redemption-h3">兑换的商品：'+data.data[i].name+'</h3>'
                        +'<h4 class="redemption-h4">兑换时间：'+otime+'</h4><em class="delete"></em> <h4 class="redemption-h4 redemption-h5"><button class="ads">填写地址</button></h4></a>';
                    }else{
                        ohtml+='<a class="mf-table-view-cell" data-order="'+data.data[i].order_id+'" ><h3 class="redemption-h3">兑换的商品：'+data.data[i].name+'</h3>'
                        +'<h4 class="redemption-h4">兑换时间：'+otime+'</h4><em class="delete"></em> </a>';
                    }
                        
                }
                $('.mui-scroll').html(ohtml);
            }else if(data.error_code == 1001){
                setTimeout(function(){
                        exitlogin();
                    },1000);
            }else{
            }
        }  
    });
}

//时间转换
function timemsg(time){
    var date = new Date(time * 1000);
    var year = date.getFullYear();
    var mou = date.getMonth() + 1;
    mou<10? mou='0'+mou :null;
    var day = date.getDate();
    day<10? day='0'+day :null;
    var hours = date.getHours();
    hours<10? hours='0'+hours :null;
    var minutes = date.getMinutes(); 
    minutes<10? minutes='0'+minutes :null;
    var newtime = year + '-' + mou + '-' + day + ' ' + hours + ':' +minutes ;
    return newtime;
}


//删除信息
function deletlist(obj){
    var orderid =obj.attr('data-order');
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/order/orderDel",
        dataType:'jsonp',
        data: { uid: uid, token: token,order_id:orderid},
        success: function(data) {
            if(data.error_code == 0){
                obj.remove()
            }else if(data.error_code == 1001){
                setTimeout(function(){
                        exitlogin();
                    },1000);
            }else{
            }
        }  
    });
}